package com.x.Servlet_01_操作流程;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * ClassName: UserServlet
 * Package: IntelliJ IDEA

 * Description:
 * 1、给Web项目配置tomcat
 * 2、重写service()方法，业务逻辑
 * 3、在web.xml中，配置Servlet对应的请求映射

 * Written by HanXu
 * Create 2024.08.01 3:12
 */


public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、从请求报文中获取请求中的任何信息
        String username = request.getParameter("username");//提取username字段传入的数据

        String info = "<h1>NO</h1>";
        //2、业务代码逻辑
        if("zhangsan".equals(username)){
            info="<h1>YES</h1>";
        }

        //设置对应的Content-Type
        //response.setHeader("Content-Type","text/html");
        response.setContentType("text/html");

        //3、将数据包装成响应报文对象response
        PrintWriter writer = response.getWriter();//该方法返回的是在响应体中打印字符的打印流
        writer.write(info);
    }
}

